package com.jy.adscm.operation.aftersales.controller;


import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.ShSettingProblem;
import com.jy.adscm.operation.aftersales.service.ShSettingProblemService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-07-04
 */
@RestController
@RequestMapping("/adscm/operation/aftersales/shSettingProblem")
public class ShSettingProblemController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    ShSettingProblemService shSettingProblemService;

    /**
     * 新增客户信息
     * @param shSettingProblem
     * @param request
     * @return
     */
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody ShSettingProblem shSettingProblem, HttpServletRequest request)throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }
        shSettingProblem.setCreator(token.getUser().getId().toString());
        shSettingProblem.setCreateTime(new Date());
        boolean insert = shSettingProblemService.insert(shSettingProblem);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    /**
     * 修改用户信息
     * @param shSettingProblem
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody ShSettingProblem shSettingProblem, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        shSettingProblem.setCreator(token.getUser().getId().toString());
        shSettingProblem.setCreateTime(new Date());
        boolean result = shSettingProblemService.updateById(shSettingProblem);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    /**
     * 根据id删除用户信息
     * @param id
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer  id, HttpServletRequest request) throws AdscmSystemException {
        boolean result = shSettingProblemService.deleteById(id);
        return  new SimpleCoreResponse("200","删除成功",result);
    }

    /**
     * 根据查询条件查询用户信息
     * @param
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectByPid/{pid}")
    public  SimpleCoreResponse  selectByPid(@PathVariable Integer pid  , HttpServletRequest request) throws  AdscmSystemException{
        List<ShSettingProblem> shSettingProblems =  shSettingProblemService.selectByPid(pid);
        return  new SimpleCoreResponse("200","查询成功",shSettingProblems);
    }


}

